package com.gip.service.Impl;

import com.gip.mapper.DataMapper;
import com.gip.mapper.OrderMapper;
import com.gip.pojo.Cost;
import com.gip.pojo.Order;
import com.gip.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    DataMapper dataMapper;

    @Override
    public int countOrders() {
        return orderMapper.countOrders();
    }

    public List<Order> findAllOrders(Map map) {
        return orderMapper.findAllOrders(map);
    }

    @Override
    public Order findOrderById(int id) {
        return orderMapper.findOrderById(id);
    }

    @Override
    public List<Order> findAllNewOrders(Order order) {
        return orderMapper.findAllNewOrders(order);
    }

    @Override
    public int updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }

    @Override
    public int deleteOrder(int id) {
        return orderMapper.deleteOrder(id);
    }

    @Override
    public int addOrder(Order order) {
        return orderMapper.addOrder(order);
    }

    @Override
    public int deleteOrders(List list) {
        return orderMapper.deleteOrders(list);
    }

    @Override
    public int rePay(Order order) {
        List<Cost> cost = dataMapper.findCost();
        HashMap<String, String> map = new HashMap<>();
        for (Cost cost1 : cost) {
            map.put(cost1.getTypeName(), cost1.getValue());
        }
        //续费处理
        //取出订单办理类型
        String processingType = order.getProcessingType();
        if(processingType.equals("包月")){
            Date maturityDate = order.getMaturityDate();

            Calendar cal = Calendar.getInstance();
            cal.setTime(maturityDate);

            // 获取年
            int year = cal.get(Calendar.YEAR);
            // 获取月
            int month = cal.get(Calendar.MONTH) + 1;
            // 获取日
            int date = cal.get(Calendar.DATE);

            month=month=1;


            //到期时间日历对象
            Calendar maturity = Calendar.getInstance();
            maturity.set(year, month, date);
            maturityDate = maturity.getTime();
            //放进roder对象里面
            order.setMaturityDate(maturityDate);

            Double monthCost = Double.valueOf(map.get("包月"));
            double cost1 = order.getCost();
            cost1=cost1+monthCost;
            order.setCost(cost1);

            int i = orderMapper.updateOrder(order);
            if(i>=1){
                return 1;
            }

        }else{
            Date maturityDate = order.getMaturityDate();

            Calendar cal = Calendar.getInstance();
            cal.setTime(maturityDate);

            // 获取年
            int year = cal.get(Calendar.YEAR);
            // 获取月
            int month = cal.get(Calendar.MONTH) + 1;
            // 获取日
            int date = cal.get(Calendar.DATE);

            year=year+1;

            //到期时间日历对象
            Calendar maturity = Calendar.getInstance();
            maturity.set(year, month, date);
            maturityDate = maturity.getTime();
            //放进roder对象里面
            order.setMaturityDate(maturityDate);

            Double yearCost = Double.valueOf(map.get("包年"));
            double cost1 = order.getCost();
            cost1=cost1+yearCost;
            order.setCost(cost1);

            int i = orderMapper.updateOrder(order);
            if(i>=1){
                return 1;
            }

        }

        return 0;
    }

}
